package com.scm.web.controller.purchase;

import com.scm.common.annotation.Log;
import com.scm.common.core.controller.BaseController;
import com.scm.common.core.domain.AjaxResult;
import com.scm.common.core.domain.entity.SysUser;
import com.scm.common.core.page.PageDomain;
import com.scm.common.core.page.TableDataInfo;
import com.scm.common.core.page.TableSupport;
import com.scm.common.enums.BusinessType;
import com.scm.common.utils.poi.ExcelUtil;
import com.scm.dto.req.purchase.SaveScmPurchaseReq;
import com.scm.dto.resp.QueryPurchasePageResp;
import com.scm.persist.model.purchase.ScmPurchase;
import com.scm.service.IScmPurchaseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 采购Controller
 *
 * @author reptilemanager
 * @date 2024-03-24
 */
@Controller
@RequestMapping("/system/Procurement")
public class ScmProcurementController extends BaseController
{
    private String prefix = "Procurement";

    @Autowired
    private IScmPurchaseService scmPurchaseService;

    @RequiresPermissions("system:Procurement:view")
    @GetMapping()
    public String purchase()
    {
        return prefix + "/purchase";
    }

    /**
     * 查询采购列表
     */
    @RequiresPermissions("system:Procurement:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ScmPurchase scmPurchase)
    {
       /* startPage();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer start = (pageDomain.getPageNum() - 1) * 10;*/
        PageDomain pageDomain = TableSupport.buildPageRequest();

        // 金额转换
        //orderPageDo.setAmountStr(AmountUtils.fenToStr(scmOrder.getAmount()));
        QueryPurchasePageResp queryPurchasePageResp = scmPurchaseService.selectScmPurchaseList(scmPurchase, pageDomain,getSysUser());
        return toTableDataInfo(queryPurchasePageResp.getTotle(),queryPurchasePageResp.getList());
    }

    /**
     * 导出采购列表
     */
    @RequiresPermissions("system:Procurement:export")
    @Log(title = "采购", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ScmPurchase scmPurchase)
    {
        List<ScmPurchase> list = scmPurchaseService.selectScmPurchaseList(scmPurchase);

        ExcelUtil<ScmPurchase> util = new ExcelUtil<ScmPurchase>(ScmPurchase.class);
        return util.exportExcel(list, "采购数据");
    }

    /**
     * 新增采购
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存采购
     */
    @RequiresPermissions("system:Procurement:add")
    @Log(title = "采购", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ScmPurchase scmPurchase)
    {
        SysUser sysUser = getSysUser();
        SaveScmPurchaseReq req = new SaveScmPurchaseReq();
        return toAjax(scmPurchaseService.insertScmPurchase(req,sysUser));
    }

    /**
     * 修改采购
     */
    @RequiresPermissions("system:Procurement:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ScmPurchase scmPurchase = scmPurchaseService.selectScmPurchaseById(id);
        mmap.put("scmPurchase", scmPurchase);
        return prefix + "/edit";
    }

    /**
     * 修改保存采购
     */
    @RequiresPermissions("system:Procurement:edit")
    @Log(title = "采购", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ScmPurchase scmPurchase)
    {
        SysUser sysUser = getSysUser();
        SaveScmPurchaseReq req = new SaveScmPurchaseReq();
        return toAjax(scmPurchaseService.updateScmPurchase(req,sysUser));
    }

    /**
     * 删除采购
     */
    @RequiresPermissions("system:Procurement:remove")
    @Log(title = "采购", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(scmPurchaseService.deleteScmPurchaseByIds(ids));
    }
}
